﻿// YouShengWenZhangTingXie.cpp : 实现文件
//

#include "stdafx.h"
#include "TeacherManager.h"
#include "YouShengWenZhangTingXie.h"
#include "afxdialogex.h"

//////////////////////////////////////////////////////////////////////
//int nOne=0;
//vector<CString> CStr1;
//vector<CString>CStr2;
//CString cstrs;
//CString strEditXie;
//////////////////////////////////////////////////////////////////////
// CYouShengWenZhangTingXie 对话框

IMPLEMENT_DYNAMIC(CYouShengWenZhangTingXie, CDialogEx)

CYouShengWenZhangTingXie::CYouShengWenZhangTingXie(CWnd* pParent /*=NULL*/)
	: CDialogEx(CYouShengWenZhangTingXie::IDD, pParent),m_bool(false),m_nIndex(0)
{

}

CYouShengWenZhangTingXie::~CYouShengWenZhangTingXie()
{
}

void CYouShengWenZhangTingXie::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_EDIT3, m_EditOne);
	DDX_Control(pDX, IDC_COMBO1, m_ComShengYin);
	DDX_Control(pDX, IDC_SLIDER1, m_SuDu);
	DDX_Control(pDX, IDC_CHECK1, m_checkTip);
	DDX_Control(pDX, IDC_EDIT4, m_etResult);
}


BEGIN_MESSAGE_MAP(CYouShengWenZhangTingXie, CDialogEx)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_KAI_SHI_TING_XIE, &CYouShengWenZhangTingXie::OnBnClickedKaiShiTingXie)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN1, &CYouShengWenZhangTingXie::OnDeltaposSpin1)
	ON_BN_CLICKED(IDC_CHECK1, &CYouShengWenZhangTingXie::OnBnClickedCheck1)
	ON_BN_CLICKED(IDC_LANG_DU, &CYouShengWenZhangTingXie::OnBnClickedLangDu)
	ON_BN_CLICKED(IDC_BUTTON1, &CYouShengWenZhangTingXie::OnBnClickedButton1)
	ON_WM_HSCROLL()
	ON_CBN_SELCHANGE(IDC_COMBO1, &CYouShengWenZhangTingXie::OnCbnSelchangeCombo1)
    ON_EN_SETFOCUS(IDC_EDIT5, &CYouShengWenZhangTingXie::OnEnSetfocusEdit5)
    ON_BN_CLICKED(IDC_BTN_NEXT, &CYouShengWenZhangTingXie::OnBnClickedNext)
END_MESSAGE_MAP()


// CYouShengWenZhangTingXie 消息处理程序


BOOL CYouShengWenZhangTingXie::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
    m_nOne = 0;

	SetDlgItemText(IDC_EDIT3, m_ListWenZhang.GetString());
	//CFont m_editFont;
	//m_editFont.CreatePointFont(266, _T("Arial"));  
	m_editFont.CreatePointFont(130, _T("Arial"));   //设置字体 
	GetDlgItem(IDC_EDIT3)->SetFont(&m_editFont);  //告知文本框显示的字体
	GetDlgItem(IDC_EDIT4)->SetFont(&m_editFont);  //告知文本框显示的字体
	GetDlgItem(IDC_TI_SHI)->SetFont(&m_editFont);  //告知文本框显示的字体
	GetDlgItem(IDC_EDIT_XIE)->SetFont(&m_editFont);  //告知文本框显示的字体
	GetDlgItem(IDC_EDIT5)->SetFont(&m_editFont);  //告知文本框显示的字体
	GetDlgItem(IDC_EDIT4)->ShowWindow(FALSE);
	GetDlgItem(IDC_TI_SHI)->ShowWindow(FALSE);
	GetDlgItem(IDC_EDIT_XIE)->ShowWindow(FALSE);
	GetDlgItem(IDC_EDIT5)->ShowWindow(FALSE);
    GetDlgItem(IDC_EDIT5)->CreateCaret(NULL);
    GetDlgItem(IDC_EDIT5)->HideCaret();

	GetDlgItem(IDC_CHECK1)->EnableWindow(FALSE);
	GetDlgItem(IDC_SPIN1)->EnableWindow(FALSE);
	GetDlgItem(IDC_SLIDER1)->EnableWindow(FALSE);
	GetDlgItem(IDC_STATIC1)->EnableWindow(FALSE);
	GetDlgItem(IDC_STATIC2)->EnableWindow(FALSE);
	GetDlgItem(IDC_STATIC3)->EnableWindow(FALSE);
	GetDlgItem(IDC_COMBO1)->EnableWindow(FALSE);

	SetDlgItemText(IDC_STATICOK, _T("0"));
	m_ComShengYin.InsertString(0, _T("美音男声"));
	m_ComShengYin.InsertString(1, _T("美音女声"));
	m_ComShengYin.SetCurSel(0);

	m_SuDu.SetRange(-10, 10);
	m_SuDu.SetPos(-1);

	SetDlgItemText(IDC_STATICSUDU, _T("0"));

	CString strEdit;
	CString str;
	vector<CString> CStr;
	GetDlgItemText(IDC_EDIT3, strEdit); // 获取指定ID的编辑框内容
	for(int i=0;i<strEdit.GetLength()-1;i++)
	{
		if(strEdit[i] == '.' || strEdit[i] == ';' || strEdit[i] == '?' || strEdit[i] == '!'  )
		{
			str+=strEdit[i];
			CStr.push_back(str);
			str = "";
			continue;
		}
		str += strEdit[i];
	}
	CStr.push_back(str);
	for(int j=0; j<CStr.size()-1;j++)
	{
		m_vectOriginalSent.push_back(CStr[j].Trim());
	}
	CString ss;
	ss.Format(_T("  %d/%d "), 1,m_vectOriginalSent.size());
	GetDlgItem(IDC_EDIT5)->SetWindowText(ss);   //EDIT控件显示数据
	GetDlgItem(IDC_TI_SHI)->SetWindowText(m_vectOriginalSent[0]);   //EDIT控件显示数据
	//初始化窗口时朗读，停止按钮不可见
	GetDlgItem(IDC_BUTTON1)->ShowWindow(FALSE);
	GetDlgItem(IDC_LANG_DU)->ShowWindow(FALSE);
	UpdateData(TRUE);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

//开始听写
void CYouShengWenZhangTingXie::OnBnClickedKaiShiTingXie()
{
	// TODO: 在此添加控件通知处理程序代码
	GetDlgItem(IDC_BUTTON1)->ShowWindow(TRUE);
	GetDlgItem(IDC_LANG_DU)->ShowWindow(TRUE);

	GetDlgItem(IDC_EDIT3)->ShowWindow(FALSE);
	GetDlgItem(IDC_EDIT_XIE)->ShowWindow(TRUE);
	GetDlgItem(IDC_EDIT4)->ShowWindow(TRUE);
	GetDlgItem(IDC_EDIT5)->ShowWindow(TRUE);
	GetDlgItem(IDC_KAI_SHI_TING_XIE)->ShowWindow(FALSE);
	//GetDlgItem(IDC_LANG_DU)->EnableWindow(FALSE);

	GetDlgItem(IDC_CHECK1)->EnableWindow(TRUE);
	GetDlgItem(IDC_SPIN1)->EnableWindow(TRUE);
	GetDlgItem(IDC_SLIDER1)->EnableWindow(TRUE);
	GetDlgItem(IDC_STATIC1)->EnableWindow(TRUE);
	GetDlgItem(IDC_STATIC2)->EnableWindow(TRUE);
	GetDlgItem(IDC_STATIC3)->EnableWindow(TRUE);
	GetDlgItem(IDC_COMBO1)->EnableWindow(TRUE);
    GetDlgItem(IDC_BTN_NEXT)->EnableWindow(TRUE);
    m_nIndex = 0;
	OnBnClickedLangDu();
}

void CYouShengWenZhangTingXie::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	if(GetDlgItem(IDC_EDIT3)->ShowWindow(TRUE))
	{
		return;
	}
	GetDlgItemText(IDC_EDIT_XIE, m_strUsrContent); // 获取指定ID的编辑框内容
	if(pNMUpDown->iDelta == -1)  // 如果此值为-1 , 说明点击了Spin的往上箭头
	{
		GetDlgItem(IDC_TI_SHI)->ShowWindow(FALSE);
		m_checkTip.SetCheck( 0 );
		m_bool = false;
		GetDlgItem(IDC_EDIT3)->ShowWindow(FALSE);
		if(m_nOne == 0)
		{
			cstrs += m_vectOriginalSent[0] + _T("\r\n") +m_strUsrContent + _T("\r\n") + _T("\r\n");
			GetDlgItem(IDC_EDIT4)->SetWindowText(cstrs);   //EDIT控件显示数据
			CStr2.push_back(cstrs);
			GetDlgItem(IDC_EDIT_XIE)->SetWindowText(_T("")); 
				
			GetDlgItem(IDC_TI_SHI)->SetWindowText(m_vectOriginalSent[1]);   //EDIT控件显示数据
			CString ss;
			ss.Format(_T("  %d/%d "), 2, m_vectOriginalSent.size());
			GetDlgItem(IDC_EDIT5)->SetWindowText(ss);   //EDIT控件显示数据
			m_nOne++;
			OnBnClickedLangDu();
			return;
		}
		if(m_nIndex > m_vectOriginalSent.size() - 2)
		{
			MessageBox(_T("已是最后一句"));
			return;
		}
			
		m_nIndex++;
		cstrs = cstrs + m_vectOriginalSent[m_nIndex] + _T("\r\n") + m_strUsrContent + _T("\r\n") + _T("\r\n");
		GetDlgItem(IDC_EDIT4)->SetWindowText(cstrs);   //EDIT控件显示数据
		CStr2.push_back(cstrs);
		GetDlgItem(IDC_EDIT_XIE)->SetWindowText(_T(""));

		m_etResult.LineScroll(m_etResult.GetLineCount());   //设置滚动条位置
		GetDlgItem(IDC_EDIT_XIE)->SetWindowText(_T(""));   //EDIT控件显示数据
		if(m_nIndex == m_vectOriginalSent.size() - 1)
		{
			GetDlgItem(IDC_TI_SHI)->SetWindowText(_T("")); 
			return;
		}
		GetDlgItem(IDC_TI_SHI)->SetWindowText(m_vectOriginalSent[m_nIndex + 1]);   //EDIT控件显示数据	
	}
	else if(pNMUpDown->iDelta == 1)  // 如果此值为, 说明点击了Spin的往下箭头
	{
		GetDlgItem(IDC_TI_SHI)->ShowWindow(FALSE);
		m_checkTip.SetCheck( 0 );
		m_bool = false;
		GetDlgItem(IDC_EDIT3)->ShowWindow(FALSE);
		if(m_nIndex < 1)
		{
			GetDlgItem(IDC_EDIT4)->SetWindowText(_T(""));   //EDIT控件显示数据
			CString ss;
			ss.Format(_T("  %d/%d "), 1, m_vectOriginalSent.size());
			GetDlgItem(IDC_EDIT5)->SetWindowText(ss);   //EDIT控件显示数据
			GetDlgItem(IDC_TI_SHI)->SetWindowText(m_vectOriginalSent[0]);   //EDIT控件显示数据
			CStr2.clear();
			cstrs = _T("");
			m_nIndex = 0;
			m_nOne = 0;
			OnBnClickedLangDu();
			return;
		}
		m_nIndex--;
		GetDlgItem(IDC_EDIT4)->SetWindowText(CStr2[m_nIndex]);   //EDIT控件显示数据
		GetDlgItem(IDC_TI_SHI)->SetWindowText(m_vectOriginalSent[m_nIndex + 1]);   //EDIT控件显示数据
	}
	CString ss;
	ss.Format(_T("  %d/%d "), m_nIndex + 2,m_vectOriginalSent.size());
	GetDlgItem(IDC_EDIT5)->SetWindowText(ss);   //EDIT控件显示数据
	OnBnClickedLangDu();
	
	*pResult = 0;

}

void CYouShengWenZhangTingXie::OnBnClickedCheck1()
{
	// TODO: 在此添加控件通知处理程序代码
	m_bool = !m_bool;
	if(GetDlgItem(IDC_EDIT3)->ShowWindow(TRUE))
	{
		return;
	}
	if(m_bool)
	{
		m_checkTip.SetCheck( 1 );
		GetDlgItem(IDC_EDIT3)->ShowWindow(FALSE);
		GetDlgItem(IDC_TI_SHI)->ShowWindow(TRUE);
	}
	else if(!m_bool)
	{
		GetDlgItem(IDC_EDIT3)->ShowWindow(FALSE);
		GetDlgItem(IDC_TI_SHI)->ShowWindow(FALSE);
	}
}

//朗读
void CYouShengWenZhangTingXie::OnBnClickedLangDu()
{
	// TODO: 在此添加控件通知处理程序代码
	OnBnClickedButton1();
	::CoInitialize(NULL);             // COM初始化
	//CLSID CLSID_SpVoice;
	//CLSIDFromProgID(L"SAPI.SpVoice", &CLSID_SpVoice);
	//IEnumSpObjectTokens *pSpEnumTokens = NULL;
	//SpEnumTokens(SPCAT_VOICES, NULL, NULL, &pSpEnumTokens);
	//ULONG ulTokensNumber = 0;
	//pSpEnumTokens->GetCount(&ulTokensNumber);
	//ISpObjectToken *pSpToken = NULL;
	//pSpEnumTokens->Item(7, &pSpToken);//上面代码可以知道语音包的序号多少	
	theApp.StopSpeak();
	//theApp.m_pSpVoice->Resume();
	CString strLangDuZhe;

	GetDlgItemText(IDC_COMBO1, strLangDuZhe);
	int iSpead = m_SuDu.GetPos() - 10;
	CString strLangDuSuDu;
	strLangDuSuDu.Format(_T("%d"), iSpead);
	theApp.SetVoiceRateByValue(strLangDuZhe, strLangDuSuDu);
	CString strEdit1;
	GetDlgItemText(IDC_TI_SHI, strEdit1); // 获取指定ID的编辑框内容
	USES_CONVERSION;
	//lpcwStr = A2CW((LPCSTR)strEdit1);
	theApp.m_pSpVoice->SetRate(m_SuDu.GetPos());
	if(strEdit1.IsEmpty())
	{
		MessageBox(_T("朗读内容不能为空!"), _T("提示"), MB_OK|MB_ICONWARNING);
		return;
	}
	theApp.m_pSpVoice->Speak(strEdit1, SPF_ASYNC, NULL); 
	//pSpEnumTokens->Release();
	::CoUninitialize();
}


void CYouShengWenZhangTingXie::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	theApp.m_pSpVoice->Speak(NULL, SPF_PURGEBEFORESPEAK, NULL);
	//theApp.m_pSpVoice->Pause();
}

void CYouShengWenZhangTingXie::OnClose()
{
	OnBnClickedButton1();
	CDialogEx::OnClose();
}

void CYouShengWenZhangTingXie::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	int Pos = m_SuDu.GetPos();
	CString str;
	str.Format(_T("%d"),Pos);
	SetDlgItemText(IDC_STATICSUDU, str);

	CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}


void CYouShengWenZhangTingXie::OnCbnSelchangeCombo1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strVoice;
	GetDlgItem(IDC_COMBO1)->GetWindowTextW(strVoice);
	int Pos = m_SuDu.GetPos();
	CString str;
	str.Format(_T("%d"), Pos);
	SetDlgItemText(IDC_STATICSUDU, str);
	theApp.SetVoiceRateByValue(strVoice, str);
}


BOOL CYouShengWenZhangTingXie::PreTranslateMessage(MSG* pMsg)
{
    // TODO: Add your specialized code here and/or call the base class
    if(pMsg->message  == WM_KEYDOWN)
    {
        if (pMsg->hwnd == GetDlgItem(IDC_EDIT5)->m_hWnd)
        {
            return  TRUE;
        }
    }

    return CDialogEx::PreTranslateMessage(pMsg);
}


void CYouShengWenZhangTingXie::OnEnSetfocusEdit5()
{
    // TODO: Add your control notification handler code here
    GetDlgItem(IDC_EDIT5)->HideCaret();
}


void CYouShengWenZhangTingXie::OnBnClickedNext()
{
    if(m_nIndex >= m_vectOriginalSent.size())
    {
        MessageBox(_T("已是最后一句"));
        return;
    }

    CString strInfo;
    // 获取指定ID的编辑框内容
    GetDlgItem(IDC_TI_SHI)->ShowWindow(FALSE);
    GetDlgItem(IDC_EDIT3)->ShowWindow(FALSE);
    m_checkTip.SetCheck( 0 );

    CString strResult;
    m_etResult.GetWindowText(strResult);
    int nPos = strResult.GetLength();
    GetDlgItemText(IDC_EDIT_XIE, m_strUsrContent); 
    strInfo = _T("原文: ") + m_vectOriginalSent[m_nIndex]+ _T("\r\n");
    nPos +=  strInfo.GetLength();
    strInfo += _T("听写: ") + m_strUsrContent.Trim() + _T("\r\n");
    strInfo += _T("=============================================\r\n");
   
    m_etResult.SetWindowText(strResult + strInfo);   //EDIT控件显示数据
    m_etResult.SetSel(nPos, nPos);

    GetDlgItem(IDC_EDIT_XIE)->SetWindowText(_T(""));

    ++m_nIndex;
    if(m_nIndex < m_vectOriginalSent.size())
    {
        GetDlgItem(IDC_TI_SHI)->SetWindowText(m_vectOriginalSent[m_nIndex]);   //EDIT控件显示数据
        CString ss;
        ss.Format(_T("  %d/%d "), m_nIndex+1, m_vectOriginalSent.size());
        GetDlgItem(IDC_EDIT5)->SetWindowText(ss);   //EDIT控件显示数据
        OnBnClickedLangDu();
    } 
    
}
